package com.suneast.suneaststore.modules.personal.address;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.suneast.suneaststore.R;
import com.suneast.suneaststore.modules.personal.address.adapter.AreaAdapter;
import com.suneast.suneaststore.bean.personal.address.AreaBean;
import com.suneast.suneaststore.modules.personal.address.event.GotAreaList;
import com.suneast.suneaststore.modules.personal.address.event.GotCurAreaList;
import com.suneast.suneaststore.modules.personal.address.event.GotProvinceList;
import com.suneast.suneaststore.ui.fragments.BaseFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class AreaSelectFragment extends BaseFragment {
    @Override
    public int setLayout() {
        return R.layout.fragment_select_area;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        EventBus.getDefault().register(this);
        initData();
        Bundle bundle = getArguments();
        addressId = bundle.getInt("id", -1);
        if (addressId != -1) {
            curAreaList.get(0).setId(bundle.getInt("pid"));
            curAreaList.get(1).setId(bundle.getInt("cid"));
            curAreaList.get(2).setId(bundle.getInt("aid"));
            curAreaList.get(3).setId(bundle.getInt("tid"));
            curAreaList.get(0).setAreaName(bundle.getString("province"));
            curAreaList.get(1).setAreaName(bundle.getString("city"));
            curAreaList.get(2).setAreaName(bundle.getString("area"));
            curAreaList.get(3).setAreaName(bundle.getString("town"));

        }
        initView();
        if (addressId != -1) {
            AddressHandler.requestCurAreaList(addressId);
        } else {
            AddressHandler.requestProvinceList();
        }
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

    @OnClick({R.id.view_back, R.id.tv_confirm})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.view_back: {
                pop();
            }
            break;
            case R.id.tv_confirm: {
                if (isSelectAll()) {//地区选择完全
                    popResult();
                } else {
                    Toast.makeText(getContext(), "请选择完全部地址", Toast.LENGTH_SHORT).show();
                }
            }
            break;
        }
    }

    private void popResult() {
        Bundle bundle = new Bundle();
        bundle.putInt("pid", curAreaList.get(0).getId());
        bundle.putInt("cid", curAreaList.get(1).getId());
        bundle.putInt("aid", curAreaList.get(2).getId());
        bundle.putInt("tid", curAreaList.get(3).getId());
        bundle.putString("province", curAreaList.get(0).getAreaName());
        bundle.putString("city", curAreaList.get(1).getAreaName());
        bundle.putString("area", curAreaList.get(2).getAreaName());
        bundle.putString("town", curAreaList.get(3).getAreaName());
        setFragmentResult(AddressEditFragment.RESULT_CODE_SUCCEED, bundle);
        pop();
    }

    int addressId = -1;
    ArrayList<AreaBean> curAreaList = null;

    private void initData() {
        curAreaList = new ArrayList<>(4);
        for (int i = 0; i < 4; i++) {
            curAreaList.add(new AreaBean());
        }
    }

    @BindView(R.id.tab_area)
    TabLayout areaTab;

    @BindView(R.id.rv_area)
    RecyclerView areaRv;
    private AreaAdapter areaAdapter;

    private void initView() {
        AddressEditFragment addressEditFragment = findFragment(AddressEditFragment.class);
        addressEditFragment.getView().setVisibility(View.VISIBLE); // 设置上一个界面可见
        areaRv.setLayoutManager(new LinearLayoutManager(getContext()));
        areaAdapter = new AreaAdapter(R.layout.item_area);
        areaAdapter.setCurId(curAreaList.get(0).getId());
        areaAdapter.setOnItemClickListener((BaseQuickAdapter adapter, View view, int position) -> {
            AreaBean bean = (AreaBean) adapter.getData().get(position);
            int level = bean.getLevel();
            curAreaList.set(level - 1, bean);
            TabLayout.Tab tab = areaTab.getTabAt(level - 1);
            tab.setText(bean.getAreaName());
            if (level != 4) {
                AddressHandler.requestAreaList(bean.getId());
                TabLayout.Tab nextTab = areaTab.getTabAt(level);
                nextTab.select();
            } else {
                if (isSelectAll()) {
                    popResult();
                }
            }
        });
        areaRv.setAdapter(areaAdapter);
        areaTab.getTabAt(0).setText(curAreaList.get(0).getAreaName());
        areaTab.getTabAt(1).setText(curAreaList.get(1).getAreaName());
        areaTab.getTabAt(2).setText(curAreaList.get(2).getAreaName());
        areaTab.getTabAt(3).setText(curAreaList.get(3).getAreaName());
        areaTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0: {//选择了省份
                        areaAdapter.setCurId(curAreaList.get(0).getId());
                        AddressHandler.requestProvinceList();
                    }
                    break;
                    case 1: {//选择了市区
                        areaAdapter.setCurId(curAreaList.get(1).getId());
                        int parentId = curAreaList.get(0).getId();
                        if (parentId != -1) {
                            AddressHandler.requestAreaList(parentId);
                        } else {
                            areaAdapter.setNewData(new ArrayList<>());
                        }
                    }
                    break;
                    case 2: {//选择了县区
                        areaAdapter.setCurId(curAreaList.get(2).getId());
                        int parentId = curAreaList.get(1).getId();
                        if (parentId != -1) {
                            AddressHandler.requestAreaList(parentId);
                        } else {
                            areaAdapter.setNewData(new ArrayList<>());
                        }
                    }
                    break;
                    case 3: {//选择了街道
                        areaAdapter.setCurId(curAreaList.get(3).getId());
                        int parentId = curAreaList.get(2).getId();
                        if (parentId != -1) {
                            AddressHandler.requestAreaList(parentId);
                        } else {
                            areaAdapter.setNewData(new ArrayList<>());
                        }
                    }
                    break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private boolean isSelectAll() {
        boolean flag = true;
        for (int i = 0, length = curAreaList.size(); i < length; i++) {
            if (curAreaList.get(i).getId() == -1) {
                flag = false;
                break;
            }
        }
        return flag;
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGotCurAreaList(GotCurAreaList event) {
        areaAdapter.setNewData(event.getProvinceList());
    }

    private ArrayList<AreaBean> areaList;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGotProvinceList(GotProvinceList event) {
        areaList = event.getProvinceList();
        areaAdapter.setNewData(areaList);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGotAreaList(GotAreaList event) {
        areaList = event.getAreaList();
        areaAdapter.setNewData(areaList);
    }

}
